package com.wl.blog.interceptor;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//继承Spring提供的拦截器,做页面访问拦截
public class LoginInerceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response,
                             Object handler) throws Exception {

         //从session中获取user对象,如果为空进行重定向
         if(request.getSession().getAttribute("user") == null ){
            response.sendRedirect("/admin");
            return false;
         }
        return true;
    }
}
